package cn.itsource.order;

import cn.hutool.core.collection.CollectionUtil;
import cn.itsource.order.domain.TOrderAdopt;
import cn.itsource.order.mapper.OrderAdoptMapper;
import org.apache.commons.lang3.time.DateUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import java.util.Date;
import java.util.List;
import java.util.stream.Stream;

@Component
public class OrderEnding {

    @Autowired
    OrderAdoptMapper orderAdoptMapper;

    @Scheduled(cron = "0 0/1 * * * ? ")
    public void Ending(){
        //业务逻辑?
        //完成关单操作
        //1.首先我们的定时器,是每一分钟就执行一次
        //2.我们通过查询条件过滤,第一个条件是状态是待支付的,第二个条件是创建订单的时间处于当前时间十五分钟之内的
        List<TOrderAdopt> tOrderAdopts = orderAdoptMapper.findByStateAndTime(new Date());
        System.out.println(tOrderAdopts);
        if (CollectionUtil.isEmpty(tOrderAdopts)) return ;
        tOrderAdopts.stream().forEach(tOrderAdopt ->{
            Date lastConfirmTime = tOrderAdopt.getLastConfirmTime();
            //这个最终生成订单的时间+15分钟后与当前时间作对比
            Date newDate = new Date();

            Date orderDate = DateUtils.addMinutes(lastConfirmTime, 15);
            //过期了
            if (newDate.after(orderDate)){
                tOrderAdopt.setState(2);
                orderAdoptMapper.updateByPrimaryKey(tOrderAdopt);
            }
        });
    }

}
